﻿using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Microsoft.Xna.Framework.Media;

namespace Epitech.Krisk.Xna.UI.Game.Utils
{
    class Sound
    {
        SoundEffect sound;
        SoundEffectInstance instance;
        ContentManager _content;
        string _name;
        ScreenManager _manager;
        public float vol { get; set; }

        private Sound(ScreenManager manager, string name)
        {
            _name = name;
            _manager = manager;
            _content = new ContentManager(_manager.Game.Services, "Content");
            MediaPlayer.Volume = 0.5f;
            vol = MediaPlayer.Volume;
        }

        public void play()
        {
            sound = _content.Load<SoundEffect>(_name);
            instance = sound.CreateInstance();

            sound.Play();
        }

        public void setMusic(string name)
        {
            _name = name;
            play();
        }

        public void setSound(int volume)
        {
            instance.Volume = (float)volume / 100.0f;
            vol = instance.Volume;
            play();
        }

        public void soundUp()
        {
            if (instance.Volume < 1.0f)
                instance.Volume += 0.01f;
            vol = instance.Volume;
            play();
        }

        public void soundDown()
        {
            if (instance.Volume > 0.0f)
                instance.Volume -= 0.0f;
            vol = instance.Volume;
            play();
        }

        static Sound ptr;

        public static Sound GetInstance(ScreenManager manager = null, string name = "Music/BackgroundMusic")
        {
            if (ptr == null && manager != null)
                ptr = new Sound(manager, name);
            return ptr;
        }

    }
}
